// pages/songList/songList.js
import {SongList} from "./songList-model.js";
var songList=new SongList();
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    songData:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.from=="index"){
      var search=options.search;
      this.searchMusic(search);
    }else if(options.from=="mymusic"){
      var loveList=wx.getStorageSync("loveList");
      songList.getUserInfo((res)=>{
        var nickName=res.nickName;
        var avatarUrl=res.avatarUrl;
        if (loveList) {
          this.setData({
            songData: {
              coverImgUrl: "http://pic.qiantucdn.com/58pic/25/86/56/5873963384d34_1024.jpg!/fw/1024/watermark/url/L2ltYWdlcy93YXRlcm1hcmsveGlhb3R1LnBuZw==/align/center",
              name: "我喜欢",
              creator: {
                nickname: nickName?nickName:"老王",
                avatarUrl: avatarUrl?avatarUrl:"../../image/tabbar/my.png",
              },
              songs: loveList
            }
          });
        }
      }); 
        app.globalData.back = true;

    }else{

      var id = options.id;
      songList.getListByID(id, (res) => {
        wx.setStorageSync("songs", res.data.Body.songs);
        this.setData({
          songData: res.data.Body
        });
      });
    }
    
  },

  //搜索
  searchMusic:function(search){
    wx.request({
      url: 'http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=' + search + '&type=1&offset=0&total=true&limit=10',
      method: "post",
      header: {
        "content-type": "application/json",
        "Host": "music.163.com"
      },
      success: (res) => {
        //console.log(res.data);
        var songs = res.data.result.songs;
        var songArr=this.dealSongs(songs);
        var list={
          name:songs[0].album.name,
          title:"搜索结果",
          coverImgUrl:songs[0].artists[0].img1v1Url,
          creator:{
            nickname:''
          },
          songs:songArr
        }
        wx.setStorageSync("songs", songArr);
        this.setData({
          songData:list
        });
      }
    });
  },

  dealSongs:function(songs){
    var songArr=[];
    songs.forEach(function(item,index){
      var arr={};
      arr.id=item.id;
      arr.title=item.name;
      arr.time=item.duration;
      arr.author=item.artists[0].name;
      arr.author_pic ="https://api.hibai.cn/music/Music/Music?id=+"+item.album.picId+"&type=pic";
      arr.pic ="https://api.hibai.cn/music/Music/Music?id="+item.id+"&type=pic";
      arr.url = "https://api.hibai.cn/music/Music/Music?id=" + item.id +"&type=url";
      arr.lrc = "https://api.hibai.cn/music/Music/Music?id=" + item.id +"&type=lrc";
      songArr.push(arr);
    });
    return songArr;
  },

  onTap:function(e){
    var index=songList.getDataSet(e,"index");
    var id=songList.getDataSet(e,"id");
    var name=this.data.songData.name;
    wx.navigateTo({
      url: '../song/song?&name='+name+"&id="+id,
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})